﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;

namespace $safeprojectname$
{
    public class ComboBox : SelectOptionControl<IPersistentObject, ComboBox>
    {
        #region members
        #endregion members

        #region constructor
        public ComboBox() { }
        public ComboBox(bool? asReadonly = null) : base(asReadonly) { }
        #endregion constructor

        #region OnSourceProperty
        protected override void OnSourcePropertyChanged(string propertyName, IPersistentObject value)
        {
            base.OnSourcePropertyChanged(propertyName, value);

            if (value.Is())
            {
                SetDetailEntityID(value.IDToDisplay);
                SetSelectedValue(value.IDToDisplay);
                SetText(value.ToDisplay());
            }
        }
        #endregion OnSourceProperty

        #region Render
        protected override void ReadControls()
        {
            Controls.Add
                (
                    new Div()
                        .SetCssClassName(GetAttribute(Attr.Class))
                        .AddControls
                        (
                            new Anchor()
                                .SetTitle(Text.Length > MaxLength ? Text : null)
                                .SetTitle(Title)
                                .SetHref("")
                                .AddControls
                                (
                                    new Literal(Text.ToLength(MaxLength, true))
                                )
                        )
                );
        }

        protected override void WriteControls()
        {
            Controls.Add
                (
                    AddSelect()
                );
        }

        protected virtual Select AddSelect()
        {
            return new Select()
                .SetCssClassName("single")
                .SetName(InputName)
                .AddControls
                (
                    AddOptions().ToArray()
                );
        }

        protected virtual IEnumerable<ISelectChild> AddOptions()
        {
            var list = new List<ISelectChild>();
            foreach (var item in Data)
            {
                list.Add
                    (
                        new Option()
                            .SetText(item.Text)
                            .SetSelected(item.Selected)
                            .SetValue(item.Value)
                    );
            }
            return list;
        }
        #endregion Render

        #region Set public
        #endregion Set public

        #region properties
        #endregion properties
    }
}